﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using intelliSys.Management.Automation.Test.Properties;
using intelliPaper.Model;
using intelliPaper.FileHandler;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Exit(object sender, ExitEventArgs e)
        {

        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (e.Args.Length == 0)
            {
                return;
            }
            else if (e.Args.Length == 1)
            {
                DataRepository.IsOpenFromFile = true;
                DataRepository.StartFilePath = e.Args[0];
            }
            else
            {
                Message.Show(null, "文件打开错误", MessageType.Comfirm, "您试图打开的文件数目出现错误，请确认，并重新打开", "文件打开数目错误");
            }
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            intelliSys.Management.Automation.Test.Properties.Settings.Default["ReturnFromException"] = true;
            Settings.Default.Save();
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                UnhandledException unhandled = new UnhandledException();
                foreach (Window w in Application.Current.Windows)
                {
                    if (w != unhandled)
                    {
                        w.Close();
                    }
                }
                if (e.Exception.GetType() == typeof(ApplicationException))
                {
                    Message.Show(null, "", MessageType.Comfirm, "This problem has occured several times and it cannot be recovered. If this is the first time you're seen this, retry your last actions. If you've seen this many times, it is likely to be caused by an application design error. Contact the developer in the restart-pending page using the 'MAIL US' link.", "LAST PROBLEM HAS OCCURED AGAIN");
                }
                unhandled.ex = e.Exception;
                unhandled.Show();

                e.Handled = true;
            }
        }
    }
}